package aiku.numericalcontroloa.Service.Impl;

import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.CurrentMapper;
import aiku.numericalcontroloa.Mapper.UserMapper;
import aiku.numericalcontroloa.Mapper.UserScoreMapper;
import aiku.numericalcontroloa.Model.Entity.Current;
import aiku.numericalcontroloa.Model.Entity.User;
import aiku.numericalcontroloa.Model.Entity.UserScore;
import aiku.numericalcontroloa.Model.Vo.WorkSummaryVo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import aiku.numericalcontroloa.Model.Entity.WorkSummary;
import aiku.numericalcontroloa.Service.WorkSummaryService;
import aiku.numericalcontroloa.Mapper.WorkSummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author 马
* @description 针对表【work_summary】的数据库操作Service实现
* @createDate 2024-03-13 17:25:47
*/
@Service
public class WorkSummaryServiceImpl extends ServiceImpl<WorkSummaryMapper, WorkSummary>
    implements WorkSummaryService{
    @Autowired
    private WorkSummaryMapper workSummaryMapper;
    @Autowired
    private CurrentMapper currentMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserScoreMapper userScoreMapper;
    @Autowired
    private WorkSummaryServiceImpl workSummaryService;

    @Override
    public Result addWorkSummary(WorkSummary workSummary) {
        String userid = StpUtil.getLoginId().toString();
        //先查询该用户当日是否添加过记录,查询当天
        LambdaQueryWrapper<WorkSummary> queryWrapper = new LambdaQueryWrapper<>();
        //获取当天时间
        LocalDate today = LocalDate.now();
        LocalDateTime startDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endDateTime = LocalDateTime.of(today, LocalTime.MAX);
        queryWrapper.between(WorkSummary::getWorkTime,startDateTime,endDateTime);
        queryWrapper.eq(WorkSummary::getUserId,userid);
        if(workSummaryMapper.selectCount(queryWrapper)>0){
            return Result.error(201,"您今日已添加过记录");
        }

        workSummary.setWorkTime(new Date());
        workSummary.setUserId(userid);
        workSummary.setScore(0);
        int i = workSummaryMapper.insert(workSummary);
        return i == 1 ? Result.success("添加成功") : Result.error(201,"添加失败");
    }

    /**
     * 查询当天用户提交的记录
     * @return
     */
    @Override
    public Result selectWorkSummaryList() {
        LocalDate today = LocalDate.now();
        LocalDateTime startDateTime = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime endDateTime = LocalDateTime.of(today, LocalTime.MAX);

        LambdaQueryWrapper<WorkSummary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(WorkSummary::getWorkTime, startDateTime, endDateTime);
        //时间越新排前面
        queryWrapper.orderByDesc(WorkSummary::getWorkTime);
        List<WorkSummary> workSummaries = workSummaryMapper.selectList(queryWrapper);
        if (workSummaries.size()<=0){
            return Result.success(new WorkSummaryVo());
        }
        //取出userid去user表查询姓名
        List<String> useridList = workSummaries.stream().map(WorkSummary::getUserId).collect(Collectors.toList());
        List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getId, useridList));
        workSummaries.forEach(workSummary -> {
            userList.forEach(user -> {
                if(workSummary.getUserId().equals(user.getId())){
                    workSummary.setUserName(user.getNickName());
                }
            });
        });

        WorkSummaryVo workSummaryVo = new WorkSummaryVo();
        workSummaryVo.setWorkSummaryList(workSummaries);
        workSummaryVo.setTotal(Long.valueOf(workSummaries.size()));
        return Result.success(workSummaries);
    }

}




